package com.xuetang9.fujian.view;

import com.xuetang9.fujian.model.User;

import javax.swing.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @describe:
 * @author:fujian
 * @date:2021/6/12
 * @Version: V1.0
 * @Package: com.xuetang9.fujian.view
 */
@SuppressWarnings("all")
public class WindowManager {
    private static RegisterFrame registerFrame;
    private static LoginFrame loginFrame;
    private static MainFrame mainFrame;
    private static ChatFrame chatFrame;
    private static Map<String,ChatFrame> chatFrames = new HashMap<>();
    public static RegisterFrame getRegisterFrame(){
        if (Objects.isNull(registerFrame)){
            return new RegisterFrame();
        }
        return registerFrame;
    }
    public static void addRegisterFrame(RegisterFrame frame){
        registerFrame = frame;
    }

    public static LoginFrame getLoginFrame(){
        return loginFrame;
    }

    public static void addLoginFrame(LoginFrame loginFrame){
        WindowManager.loginFrame = loginFrame;
    }

    public static MainFrame getMainFrame(){
        return mainFrame;
    }

    public static void addMainFrame(MainFrame frame){
        mainFrame = frame;
    }

    public static ChatFrame getChatFrame(User user){
        ChatFrame chatFrame = null;
        if (chatFrames.containsKey(user.getAccount())){
            chatFrame = chatFrames.get(user.getAccount());
        }else {
            chatFrame = new ChatFrame(user);
            chatFrames.put(user.getAccount(),chatFrame);
        }
        chatFrame.setExtendedState(JFrame.NORMAL);
        return chatFrame;
    }

    public static void closeChatFrame(String account){
        if (chatFrames.containsKey(account)){
            chatFrames.remove(account);
        }
    }

}
